const mongoose = require("mongoose");

const TaskCommentSchema = new mongoose.Schema(
  {
    task: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Task",
      required: true,
    },
    author: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      required: true,
    },
    content: {
      type: String,
      required: true,
      trim: true,
      maxlength: 1000,
    },
    // 评论类型：普通评论、状态变更、系统通知等
    type: {
      type: String,
      enum: ["comment", "status_change", "assignment", "system"],
      default: "comment",
    },
    // 如果是状态变更，记录变更详情
    statusChange: {
      from: String,
      to: String,
    },
    // 如果是分配变更，记录分配详情
    assignmentChange: {
      from: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "User",
      },
      to: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "User",
      },
    },
    // 是否已读（用于通知）
    isRead: {
      type: Boolean,
      default: false,
    },
  },
  { timestamps: true }
);

// 索引优化
TaskCommentSchema.index({ task: 1, createdAt: -1 });
TaskCommentSchema.index({ author: 1 });

const TaskComment = mongoose.model("TaskComment", TaskCommentSchema);

module.exports = TaskComment;
